try{
	Type.registerNamespace('NCMMRefModel');
	NCMMRefModel.StockorgRefModel={
		refname:"库存组织",
		tblname:"app_org_stockorg",
		hiddenfieldcode:["pk_stockorg"],
		fieldcode:["code","name"],
		fieldname:["工厂编码","工厂名称"],
		pkfield:"pk_stockorg",
		showfield:"name"
	};
	NCMMRefModel.registerRefInfo=function(topdivid,refinfos){
		$ctx.put("ncmm_refinfos",refinfos);
		for(reftextid in refinfos){
			var new_ele = $document.createElement("label");
			new_ele.set("id",reftextid+"_refvalue");
			new_ele.set("hidden","hidden");
			new_ele.set("bindfield",$id(reftextid).get("bindfield"));
			$id(topdivid).appendChild(new_ele);
			$id(reftextid).set("bindfield",$id(reftextid).get("bindfield")+"_refname");
			$id(reftextid).set("onchange","NCMMRefModel.refTextOnchange('"+reftextid+"')");
			$id(reftextid).set("onlongclick","NCMMRefModel.openRefDialog('"+reftextid+"')");
		}
	};
	NCMMRefModel.refTextOnchange=function(reftextid){
		var refname=$id(reftextid).get("value");
		var ncmm_crtrefobj=$ctx.get("ncmm_crtrefobj");
		if(ncmm_crtrefobj==null||ncmm_crtrefobj==""||typeof(ncmm_crtrefobj)=="undefined"){
			var refmodel=$ctx.getJSONObject("ncmm_refinfos")[reftextid];
			NCMMRefModel.queryRefValueByName(refmodel,$id(reftextid).get("value"),function(value){
				$id(reftextid+"_refvalue").set("value",value);	
			});
		}else{
			ncmm_crtrefobj=$ctx.getJSONObject("ncmm_crtrefobj");
			var refpkfield=$ctx.getJSONObject("ncmm_refinfos")[reftextid].pkfield;
			$id(reftextid+"_refvalue").set("value",ncmm_crtrefobj[refpkfield]);			
		}
	}
	NCMMRefModel.openRefDialog=function(reftextid){
		var refmodel=$ctx.getJSONObject("ncmm_refinfos")[reftextid];
	 	$window.showModalDialog({
	 		dialogId:"com.yonyou.ncmm_pac_pda.RefModelDialog",
	 		arguments:refmodel,
	 		features:{
      			dialogLeft:20,
       			dialogTop:20,
       			dialogWidth:300,
       			dialogHeight:400
       		},
	 		callback:function(sender,args){
	 			var data=$stringToJSON(args["data"]);
	 			var refname=data[refmodel.showfield];
	 			$id(reftextid).set("value",refname);
	 			$ctx.put("ncmm_crtrefobj",data);
	 			NCMMRefModel.refTextOnchange(reftextid);
	 		}
	 	});
	};
	NCMMRefModel.queryRefDatas=function(refmodel){
		var sql="select ";
		for(var i=0;i<refmodel.hiddenfieldcode.length;i++){
			sql=sql+refmodel.hiddenfieldcode[i]+",";
		}
		for(var i=0;i<refmodel.fieldcode.length;i++){
			sql=sql+refmodel.fieldcode[i]+",";
		}
		sql=sql.slice(0,-1);
		sql=sql+" from "+refmodel.tblname;
		var param = {
			db : NCMMPubUtil.DatabaseName,
			sql : sql,
			pageIndex : 0,
			pageSize : NCMMPubUtil.PageSize
		}
		var data = $sqlite.queryByPage(param);
		data=$stringToJSON(data);
		for(var i=0;i<data.length;i++){
			var refinfodetail="";
			for(var j=0;j<refmodel.fieldcode.length;j++){
				refinfodetail=refinfodetail+refmodel.fieldname[j]+":"+data[i][refmodel.fieldcode[j]]+"\n";
			}
			data[i]["lbl_refinfodetail"]=refinfodetail;
		}
		var json={list_refinfos:data};
		$ctx.push(json);
	};
	NCMMRefModel.queryRefValueByName=function(refmodel,name,callback){
		var sql="select "+refmodel.pkfield+" from "+refmodel.tblname+" where "+refmodel.showfield+"='"+name+"'";
		var param = {
			db : NCMMPubUtil.DatabaseName,
			sql : sql,
			pageIndex : 0,
			pageSize : NCMMPubUtil.PageSize
		}
		var data = $sqlite.queryByPage(param);
		data=$stringToJSON(data);
		var value=data[0][refmodel.pkfield];
		if(callback!=null&&typeof(callback)!=undefined&&typeof(callback)=='function'){
			callback(value);
		}
	}
}catch(e){$e(e);}